package cn.com.zsw.gblog.modules.gblog.service.impl;

import cn.com.zsw.gblog.modules.gblog.dto.CategoryDTO;
import cn.com.zsw.gblog.modules.gblog.entity.GbCategoryEntity;
import cn.com.zsw.gblog.modules.gblog.entity.GbPostsEntity;
import cn.com.zsw.gblog.modules.gblog.mapper.GbCategoryMapper;
import cn.com.zsw.gblog.modules.gblog.mapper.GbPostsMapper;
import cn.com.zsw.gblog.modules.gblog.service.GbCategoryService;
import cn.com.zsw.gblog.utils.DozerBeanUtils;
import cn.com.zsw.gblog.modules.gblog.vo.CategoryVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * (GbCategory)表服务实现类
 *
 * @author shiwangzhou
 * @since 2020-06-24 11:43:15
 */
@Service("gbCategoryService")
public class GbCategoryServiceImpl extends ServiceImpl<GbCategoryMapper,
        GbCategoryEntity> implements GbCategoryService {
    @Resource
    private GbCategoryMapper gbCategoryDao;
    @Resource
    private GbPostsMapper gbPostsMapper;


    /**
     * @Author: shiwangZhou
     * @Description: 获取分类列表
     * @Date: 2020-06-24 11:47
     * @Return java.util.List<cn.com.zsw.gblog.entity.GbCategory>
     * @Param
     */
    @Override
    public List<CategoryVo> getCategoryList() {
        List<GbCategoryEntity> gbCategories = gbCategoryDao.selectList(Wrappers.<GbCategoryEntity>lambdaQuery().eq(GbCategoryEntity::getStatus, 1)
                .orderByAsc(GbCategoryEntity::getSort));
//        List<GbCategoryEntity> collect = gbCategories.parallelStream().peek(gbCategoryEntity -> gbCategoryEntity.setPostCount(
//                gbPostsMapper.selectCount(Wrappers.<GbPostsEntity>lambdaQuery().like(GbPostsEntity::getCategory, gbCategoryEntity.getTitle()))
//        )).collect(Collectors.toList());
        List<CategoryVo> collect = gbCategories.parallelStream().map(gbCategoryEntity -> {
            CategoryVo copy = DozerBeanUtils.copy(gbCategoryEntity, CategoryVo.class);
            copy.setPostCount(gbPostsMapper.selectCount(Wrappers.<GbPostsEntity>lambdaQuery().like(GbPostsEntity::getCategory, gbCategoryEntity.getTitle())));
            return copy;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * desc: 获取分类名列表
     *
     * @param
     * @return java.util.List<java.lang.String>
     * @author: shiwangZhou
     * @date: 2020-11-23 17:14
     */
    @Override
    public List<String> getCategoryNameList() {
        List<String> gbCategories = gbCategoryDao.selectList(Wrappers.<GbCategoryEntity>lambdaQuery().eq(GbCategoryEntity::getStatus, 1)
                .orderByAsc(GbCategoryEntity::getSort)).stream().map(GbCategoryEntity::getTitle).collect(Collectors.toList());
        return gbCategories;
    }

    /**
     * desc: 新增文章分类名
     *
     * @param title 分类名
     * @return void
     * @author: shiwangZhou
     * @date: 2020-10-22 20:11
     */
    @Override
    public void add(String title) {
        GbCategoryEntity gbCategoryEntity = new GbCategoryEntity();
        gbCategoryEntity.setTitle(title);
        gbCategoryEntity.setSort(0);
        gbCategoryEntity.setCreateTime(new Date());
        gbCategoryEntity.setUpdateTime(new Date());
        gbCategoryEntity.setStatus(1);
        gbCategoryDao.insert(gbCategoryEntity);
    }

    /**
     * @param categoryDTO 分类新增或者更新
     * @return void
     * @author shiwangzhou
     * @date 2021-06-03 11:24
     **/
    @Override
    public GbCategoryEntity save(CategoryDTO categoryDTO) {
        GbCategoryEntity gbCategoryEntity;
        if (StringUtils.isNotBlank(categoryDTO.getCode())) {
            gbCategoryEntity = gbCategoryDao.selectOne(Wrappers.<GbCategoryEntity>lambdaQuery()
                    .eq(GbCategoryEntity::getCode, categoryDTO.getCode()));
            BeanUtils.copyProperties(categoryDTO, gbCategoryEntity);
        } else {
            gbCategoryEntity = new GbCategoryEntity();
            BeanUtils.copyProperties(categoryDTO, gbCategoryEntity);
            gbCategoryEntity.setCode("Category:"+ UUID.randomUUID());
            gbCategoryEntity.setCreateTime(new Date());
            gbCategoryEntity.setStatus(1);
        }
        gbCategoryEntity.setUpdateTime(new Date());
        saveOrUpdate(gbCategoryEntity);
        return gbCategoryEntity;
    }

    /**
     * desc: 更新分类文章数
     *
     * @param title 分类
     * @return void
     * @author: shiwangZhou
     * @date: 2020-10-23 9:05
     */
    @Override
    public void updatePostCount(String title) {

    }
}